<?php
namespace Home\Model;
use Think\Model;
class GrouponInfoModel extends CommonModel {
	
	
	public function getGponUserList($gpon_id){
		$db_res = $this->query("SELECT gi.organiser,gd.user_id,gd.gpon_id,wui.headimgurl,wui.nickname,ui.user_tel 
		FROM ar_shop.groupon_detail AS gd 
		LEFT JOIN ar_shop.groupon_info AS gi ON gi.gpon_id = gd.gpon_id
		LEFT JOIN ar_shop.wechat_user_info as wui ON wui.user_id = gd.user_id
		LEFT JOIN ar_shop.user_info as ui ON ui.user_id = gd.user_id
		WHERE gd.gpon_id = '".$gpon_id."'
		ORDER BY gd.insert_time ASC");
		if(!empty($db_res)){
			$res = array(); 
			foreach($db_res as $db_one){
				if(empty($db_one['headimgurl'])){
					$db_one['headimgurl'] = C('DEFAULT_HEAD_IMG');
				}
				$res[] = $db_one;
			}
			$db_res = $res;
		}
		return $db_res;
	}
	
	//1:-1,5:88
	public function hitGponPrice($price_gradient,$people_num){
		$tmp_arr = explode(",",$price_gradient);
		$hit_price = -1;
		foreach($tmp_arr as $tmp){
			$gradient = explode(":",$tmp);
			if($people_num>=$gradient[0]){
				$hit_price = $gradient[1];
			}
		}
		return $hit_price;
	}
	
	public function getGponPeopleNum($gpon_id){
		$grouponDetail = D('GrouponDetail');
		$db_res = $grouponDetail->where("gpon_id = '".$gpon_id."'")->select();
		return count($db_res);
	}
	
	//用户团购状态 1：没参团 2：参团且未满 3：参团并满团  4：仅下单 5：支付完成 20售罄 22过期
	public function getUserStatus($user_id,$gpon_id,$set_id){
		$status = 1;
		
		$grouponDetail = D('GrouponDetail');
		$db_res = $grouponDetail->where("user_id = '".$user_id."' and gpon_id = '".$gpon_id."'")->select();
		if(empty($db_res)) return $status;
		$status = 2;
		$db_res = $grouponDetail->where("gpon_id = '".$gpon_id."'")->select();
		$gpon_people_num = count($db_res);
		$grouponSetting = D('GrouponSetting');
		$set_info = $grouponSetting->where("set_id = ".$set_id)->find();
		if($gpon_people_num<num_f($set_info['people_limit'])) return $status;
		$status = 3;
		$orderInfo = D('OrderInfo');
		$order_info = $orderInfo->where("order_type = 'groupon' and user_id = '".$user_id."' and order_status <> 20 and promote_id = '".$set_id."' and promote_code = '".$gpon_id."'")->find();
		if(empty($order_info)) return $status;
		$status = 4;
		$paymentInfo = D('PaymentInfo');
		$payment_info = $paymentInfo->where("order_id = '".$order_info['order_id']."'")->find();
		if($payment_info['payment_status'] != '10') return $status;
		$status = 5;
		return $status;
	}
	
	public function buildGpon($user_id,$set_id){
		$grouponInfo = D('GrouponInfo');
		$grouponDetail = D('GrouponDetail');
		$info['set_id'] = $set_id;
		$info['organiser'] = $user_id;
		$info['status'] = 0;
		$gpon_id = $grouponInfo->data($info)->add();
		$detail['user_id'] = $user_id;
		$detail['gpon_id'] = $gpon_id;
		$grouponDetail->data($detail)->add();
		return $gpon_id;
	}
	
	public function joinGpon($user_id,$gpon_id,$set_info){
		$grouponDetail = D('GrouponDetail');
		$wechatApi = D('WechatApi');
		$detail['user_id'] = $user_id;
		$detail['gpon_id'] = $gpon_id;
		$grouponDetail->data($detail)->add();
		//验证是否满团
		// $db_res = $grouponDetail->where("gpon_id = ".$gpon_id)->select();
		$db_res = $grouponDetail->query("select * from ar_shop.groupon_detail as gd 
		left join ar_shop.wechat_user_info as wui on gd.user_id = wui.user_id 
		where gd.gpon_id = '".$gpon_id."'");
		if(count($db_res)>=num_f($set_info['people_limit'])){//满团
			//更改状态
			$grouponInfo = D('GrouponInfo');
			$grouponInfo->where("gpon_id = ".$gpon_id)->save(array('status'=>1));
			$userInfo = D('UserInfo');
			//发送消息
			$ginfo = $grouponDetail->query("select * from ar_shop.groupon_info as gi 
			left join ar_shop.wechat_user_info as wui on gi.organiser = wui.user_id			
			where gi.gpon_id = '".$gpon_id."'");
			Vendor('Alidayu.TopSdk');
			foreach($db_res as $db_one){
				if(empty($db_one['openid'])) continue;
				//发微信消息
				$url = C('HTTP_DOMAIN') .'/Home/Router/gpon?setid='.$set_info['set_id'].'&page=home&gponid='.$gpon_id;
				$title = '恭喜您，您已经成功满团，请点击详情查看购买商品';
				$remark = '您参与的抢鲜团商品数量有限，先付先得，请抓紧时间购买。';
				$wechatApi->grouponSendMsg($db_one['openid'],$url,$title,$set_info['set_alias'],$ginfo[0]['nickname'],$remark);
				
				//发短信
				$dbuser = $userInfo->query("select * from ar_shop.user_info as ui left join ar_shop.wechat_user_info as wui on wui.user_id = ui.use_id where wui.openid = '".$db_one['openid']."'");
				$c = new \TopClient;
				$c->appkey = C('ALIDAYU_CONFIG.app_key');
				$c->secretKey = C('ALIDAYU_CONFIG.app_secret');
				$req = new \AlibabaAliqinFcSmsNumSendRequest;
				$req->setSmsType("normal");
				$req->setSmsFreeSignName("食市");
				$req->setSmsParam("{\"name\":\"".$set_info['set_alias']."\"}");
				$req->setRecNum($dbuser[0]['user_tel']);
				$req->setSmsTemplateCode("SMS_3980109");
				$updateData['result_detail'] = $c->execute($req);
			}
		}
		return $gpon_id;
	}
	
	public function hotGponList($set_id){
		$setting = D('GrouponSetting');
		$set_info = $setting->where("set_id = ".$set_id)->find();
		$db_res = $this->query("SELECT gd.gpon_id,gi.organiser,gi.set_id,count(*) AS gpon_count,wui.headimgurl,wui.nickname 
		FROM ar_shop.groupon_detail AS gd
		LEFT JOIN ar_shop.groupon_info AS gi ON gi.gpon_id = gd.gpon_id
		LEFT JOIN ar_shop.wechat_user_info as wui ON wui.user_id = gi.organiser
		WHERE gi.set_id = '".$set_id."' and gi.status = 0
		GROUP BY gd.gpon_id ORDER BY gpon_count DESC LIMIT 20");
		//缺微信头像
		if(empty($db_res)) return null;
		$list = array();
		foreach($db_res as $db_one){
			$db_one['people_limit'] = $set_info['people_limit'];
			$db_one['set_alias'] = $set_info['set_alias'];
			$db_one['set_name'] = $set_info['set_name'];
			$list[] = $db_one;
		}
		return $list;
	}
	
	// 前一个月
	public function myGponList($user_id){
		// 1：没参团 2：参团且未满 3：参团并满团  4：仅下单 5：支付完成 20售罄 22过期
		$date_time = date("Y-m-d",strtotime("last month"));
		$db_res = $this->query("SELECT gi.organiser,gi.`status` AS status2,gi.gpon_id,
		gs.set_id,gs.set_alias,gs.set_name,gs.people_limit,gs.death_time,
		pi.payment_status,
		wui.headimgurl,wui.nickname 
		FROM ar_shop.groupon_detail AS gd
		LEFT JOIN ar_shop.groupon_info AS gi ON gi.gpon_id = gd.gpon_id
		LEFT JOIN ar_shop.groupon_setting AS gs ON gs.set_id = gi.set_id
		LEFT JOIN ar_shop.wechat_user_info AS wui ON wui.user_id = gi.organiser
		LEFT JOIN ar_shop.order_info AS oi ON oi.promote_code = gi.gpon_id
		LEFT JOIN ar_shop.payment_info AS pi ON pi.order_id = oi.order_id
		WHERE gd.user_id = '".$user_id."' and gd.insert_time > '".$date_time."' and (oi.order_type='groupon' or oi.order_type is null) and (oi.user_id='".$user_id."' or oi.user_id is null)
		group by gd.gpon_id
		");
		// dump($db_res);exit;
		if(empty($db_res)) return null;
		$list = array();
		$gpon_ids = array();
		foreach($db_res as $db_one){
			$db_one['gpon_status'] = 2;
			if($db_one['payment_status']=='10'){
				$db_one['gpon_status'] = 5;
				unset($db_one['status2']);
				unset($db_one['death_time']);
				unset($db_one['payment_status']);
				$list[] = $db_one;
				$gpon_ids[] = $db_one['gpon_id'];
				continue;
			}
			if($db_one['death_time']<date("Y-m-d H:i:s")){
				$db_one['gpon_status'] = 22;
				unset($db_one['status2']);
				unset($db_one['death_time']);
				unset($db_one['payment_status']);
				$list[] = $db_one;
				$gpon_ids[] = $db_one['gpon_id'];
				continue;
			}
			if($db_one['status2']==1){
				$db_one['gpon_status'] = 3;
				unset($db_one['status2']);
				unset($db_one['death_time']);
				unset($db_one['payment_status']);
				$list[] = $db_one;
				$gpon_ids[] = $db_one['gpon_id'];
				continue;
			}
			unset($db_one['status2']);
			unset($db_one['death_time']);
			unset($db_one['payment_status']);
			$list[] = $db_one;
			$gpon_ids[] = $db_one['gpon_id'];
		}
		
		$in = "";
		foreach($gpon_ids as $gpon_id){
			if($in == "") $in .="'".$gpon_id."'";
			else $in .=",'".$gpon_id."'";
		}
		
		$db_res = $this->query("SELECT gpon_id,count(*) AS gpon_count FROM ar_shop.groupon_detail where gpon_id in (".$in.") group by gpon_id");
		$gpon_num_arr = array();
		foreach($db_res as $db_one){
			$gpon_num_arr[$db_one['gpon_id']] = num_f($db_one['gpon_count']);
		}
		$newlist = array();
		foreach($list as $one){
			$one['gpon_count'] = $gpon_num_arr[$one['gpon_id']];
			$newlist[] = $one;
		}
		// dump($newlist);exit;
		return $newlist;
	}
}

?>